package com.tuananh.radnom.persist.imp.xml;

import java.util.ArrayList;

import com.ajexperience.utils.DeepCopyException;
import com.ajexperience.utils.DeepCopyUtil;
import com.tuananh.radnom.model.NeGroup;
import com.tuananh.radnom.model.NetElement;
import com.tuananh.radnom.model.NetElementConfiguration;
import com.tuananh.radnom.persist.INetGroupDAO;

public class NetGroupXmlDAO extends XmlDAO implements INetGroupDAO {
	NetElementConfiguration netConfig;

	public NetGroupXmlDAO() {
		super(NetElementConfiguration.path);
		configuration = XmlConfigHolder.neConfig;
		try {
			savedConfiguration = (new DeepCopyUtil()).deepCopy(configuration);
		} catch (DeepCopyException e) {
			// TODO Auto-generated catch block
			savedConfiguration = new NetElementConfiguration();
			e.printStackTrace();
		}
		netConfig = (NetElementConfiguration) configuration;
	}
	public boolean add(NeGroup neGroup) {
		// TODO Auto-generated method stub
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		for(NeGroup group:listGroup){
			if (group.getName().equals(neGroup)) return false;
		}
		listGroup.add(neGroup);
		//reportEvent(neGroup, "add");
		return true;
	}

	public boolean update(NeGroup neGroup) {
		// TODO Auto-generated method stub
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		for(int i=0;i<listGroup.size();i++)
		{
			if (listGroup.get(i).getName().equals(neGroup.getName()))
			{
				listGroup.set(i, neGroup);
				//reportEvent(neGroup, "update");
				return true;
			}
		}
		return false;
	}

	public boolean remove(NeGroup neGroup) {
		// TODO Auto-generated method stub
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		for(NeGroup group:listGroup)
			if (group.getName().equals(neGroup.getName()))
			{
				listGroup.remove(group);
				//reportEvent(neGroup, "add");
				return true;
			}
		return false;
	}

	public ArrayList<NeGroup> getAll() {
		// TODO Auto-generated method stub	
		return netConfig.getListGroup();
	}

	public NeGroup getByName(String name) {
		// TODO Auto-generated method stub
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		for(NeGroup group:listGroup)
			if (group.getName().equals(name))
			{
				return group;
			}
		return null;
	}
	public ArrayList<NeGroup> findByNodeName(String name) {
		// TODO Auto-generated method stub
		ArrayList<NeGroup> results = new ArrayList<NeGroup>();
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		for(NeGroup group:listGroup)
			for(NetElement ne:group.getNeList())
			{
				if (ne.getName().equals(name)) {
					results.add(group);
					break;
				}
			}
		return results;
	}
	public ArrayList<NeGroup> findByNodeName(ArrayList<String> names) {
		// TODO Auto-generated method stub
		ArrayList<NeGroup> results = new ArrayList<NeGroup>();
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		for(NeGroup group:listGroup)
			for(NetElement ne:group.getNeList())
			{
				if (names.contains(ne.getName())) {
					results.add(group);
					break;
				}
			}
		return results;
	}
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "NetGroupEvent";
	}
	public ArrayList<NeGroup> getByNames(String names) {
		// TODO Auto-generated method stub
		if (names==null) return null;
		ArrayList<NeGroup> listGroup = netConfig.getListGroup();
		ArrayList<NeGroup> result = new ArrayList<NeGroup>();
		for(NeGroup group:listGroup)
			if (names.contains(group.getName()))
			{
				result.add(group);
			}
		return result;
	}
}
